package com.netease.nie.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.netease.nie.base.utils.CopyUtils;
import com.netease.nie.constants.IsUsedFlagConstants;
import com.netease.nie.dao.ExchangeGoodsInfoDao;
import com.netease.nie.dao.LimitedTimeExchangeGoodsInfoDao;
import com.netease.nie.dao.LimitedTimeExchangeSettingDao;
import com.netease.nie.dto.LimitedTimeExchangeCacheDto;
import com.netease.nie.dto.LimitedTimeExchangeDto;
import com.netease.nie.model.ExchangeGoodsInfo;
import com.netease.nie.model.LimitedTimeExchangeGoodsInfo;
import com.netease.nie.model.LimitedTimeExchangeGoodsInfoResult;
import com.netease.nie.model.LimitedTimeExchangeGoodsSetting;


/**
 * 
 * @author linzijie
 * @createDate Dec 1st,2016
 *
 */
@Service
public class LimitedTimeExchangeServiceImpl implements LimitedTimeExchangeService {

	@Autowired
	private LimitedTimeExchangeGoodsInfoDao limitedTimeExchangeGoodsInfoDao;
	
	@Autowired
	private LimitedTimeExchangeSettingDao limitedTimeExchangeSettingDao;
	
	public LimitedTimeExchangeCacheDto queryLimitedTimeExchange(){
		
		LimitedTimeExchangeCacheDto limitedTimeExchangeCacheDto = new LimitedTimeExchangeCacheDto();
		Long currentTime = System.currentTimeMillis();
		LimitedTimeExchangeGoodsSetting limitedTimeExchangeGoodsSetting = limitedTimeExchangeSettingDao.queryLimitedTimeExchangeGoodsSetting();
		
		if(limitedTimeExchangeGoodsSetting == null) {
			limitedTimeExchangeCacheDto.setIsUsed(IsUsedFlagConstants.DISABLED.getCode());
			return limitedTimeExchangeCacheDto;
		}
		
		limitedTimeExchangeCacheDto.setIsUsed(limitedTimeExchangeGoodsSetting.getIsUsed());
		
		if(limitedTimeExchangeGoodsSetting.getIsUsed() == IsUsedFlagConstants.ENABLED.getCode()){      //若入口启用
			limitedTimeExchangeCacheDto.setIsUsed(IsUsedFlagConstants.ENABLED.getCode());
			List<LimitedTimeExchangeGoodsInfoResult> limitedTimeExchangeGoodsInfoList = limitedTimeExchangeGoodsInfoDao.queryLimitedTimeExchangeGoodsList();
			if(limitedTimeExchangeGoodsInfoList == null || limitedTimeExchangeGoodsInfoList.isEmpty()){   //列表为空，展示备用图
				limitedTimeExchangeCacheDto.setThumbnailWapUrl(limitedTimeExchangeGoodsSetting.getCoverBackupWapUrl());
				limitedTimeExchangeCacheDto.setThumbnailWebUrl(limitedTimeExchangeGoodsSetting.getCoverBackupWebUrl());			
			}else{          //列表不为空，寻找正在启用的限时兑换商品
				LimitedTimeExchangeGoodsInfoResult firstGoods = limitedTimeExchangeGoodsInfoList.get(0);
				if(firstGoods.getIsUsed() == IsUsedFlagConstants.ENABLED.getCode()){         //若有商品正在启用
					if(firstGoods.getPutOffTime() < currentTime){
						limitedTimeExchangeCacheDto.setIsUsed(IsUsedFlagConstants.DISABLED.getCode());     //出错辣，屏蔽掉
						return limitedTimeExchangeCacheDto;
					}
					CopyUtils.copy(limitedTimeExchangeCacheDto, firstGoods);
					limitedTimeExchangeCacheDto.setExchangeGoodsId(firstGoods.getExchangeGoodsId());
					limitedTimeExchangeCacheDto.setThisPutOffTime(firstGoods.getPutOffTime());      //该商品的下架时间					
				}else{              //没有启用的商品，则搜索下一个将要上架的商品
					int limitedTimeExchangeGoodsInfoListSize = limitedTimeExchangeGoodsInfoList.size();
					for(int i = 0; i < limitedTimeExchangeGoodsInfoListSize ; i++){
						LimitedTimeExchangeGoodsInfoResult goods = limitedTimeExchangeGoodsInfoList.get(i);
						if(goods.getPutOnTime() != null){
							if(goods.getPutOnTime() > currentTime){      //找到列表中第一个准备上架的商品
								CopyUtils.copy(limitedTimeExchangeCacheDto, goods);
								limitedTimeExchangeCacheDto.setExchangeGoodsId(firstGoods.getExchangeGoodsId());
								limitedTimeExchangeCacheDto.setNextPutOnTime(goods.getPutOnTime());      //下一个商品的上架时间					
								limitedTimeExchangeCacheDto.setIsUsed(IsUsedFlagConstants.ENABLED.getCode());							
								break;
							}	
						}
					}
					if(limitedTimeExchangeCacheDto.getNextPutOnTime() == null){   //若不存在待上架限时活动商品，则展示备用图
						limitedTimeExchangeCacheDto.setThumbnailWapUrl(limitedTimeExchangeGoodsSetting.getCoverBackupWapUrl());
						limitedTimeExchangeCacheDto.setThumbnailWebUrl(limitedTimeExchangeGoodsSetting.getCoverBackupWebUrl());		
					}					
				}				
			}	
			
		}else{ 
			limitedTimeExchangeCacheDto.setIsUsed(IsUsedFlagConstants.DISABLED.getCode());	 //入口不启用		
		}		
		return limitedTimeExchangeCacheDto;
	}
}
